package com.ruoyi.spspublic.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.spspublic.domain.TMisReview;
import com.ruoyi.spspublic.service.ITMisReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.ruoyi.common.utils.uuid.IdUtils.simpleUUID;

/**
 * 检阅信息Controller
 *
 * @author wangwei
 * @date 2023-06-23
 */
@RestController
@RequestMapping("/spspublic/review")
public class TMisReviewController extends BaseController {
    @Resource
    private ITMisReviewService tMisReviewService;

    /**
     * 查询检阅信息列表
     */
    // @PreAuthorize("@ss.hasPermi('spspublic:review:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMisReview tMisReview) {
        startPage();
        List<TMisReview> list = tMisReviewService.selectTMisReviewList(tMisReview);
        return getDataTable(list);
    }

    /**
     * 导出检阅信息列表
     */
    //    @PreAuthorize("@ss.hasPermi('spspublic:review:export')")
    @Log(title = "检阅信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMisReview tMisReview) {
        List<TMisReview> list = tMisReviewService.selectTMisReviewList(tMisReview);
        ExcelUtil<TMisReview> util = new ExcelUtil<TMisReview>(TMisReview.class);
        return util.exportExcel(list, "review");
    }

    /**
     * 获取检阅信息详细信息
     */
    //  @PreAuthorize("@ss.hasPermi('spspublic:review:query')")
    @GetMapping(value = "/{ctId}")
    public AjaxResult getInfo(@PathVariable("ctId") String ctId) {
        return AjaxResult.success(tMisReviewService.selectTMisReviewById(ctId));
    }

    /**
     * 新增检阅信息
     */
    //    @PreAuthorize("@ss.hasPermi('spspublic:review:add')")
    @Log(title = "检阅信息", businessType = BusinessType.INSERT)
    @PostMapping
/*
  @SaveDataScope
*/
    public AjaxResult add(@RequestBody TMisReview tMisReview) {
        // 设置唯一id
        tMisReview.setCtId(simpleUUID());
        // 设置 签字人部门，签字人
        SysUser User = SecurityUtils.getLoginUser().getUser();
        /* tMisReview.setCtBranchcode(User.getManagementOrg());*/
        /* tMisReview.setCtBranchname(User.getManagementOrgName());*/
        tMisReview.setCtUsercode(User.getUserName());
        tMisReview.setCtUsername(User.getNickName());
        // 校验上一次
        List<TMisReview> lastview = tMisReviewService.selectTMisReviewLast(tMisReview);
        if (lastview.size() > 0) {
            // 存在数据,直接返回错误
            return AjaxResult.error("五分钟内不允许二次检阅，谢谢！");
        }

        // 根据是否检阅新增检阅通知
        tMisReviewService.insertTMisMessage(tMisReview);

        return toAjax(tMisReviewService.insertTMisReview(tMisReview));
    }

    /**
     * 修改检阅信息
     */
    //    @PreAuthorize("@ss.hasPermi('spspublic:review:edit')")
    @Log(title = "检阅信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TMisReview tMisReview) {
        return toAjax(tMisReviewService.updateTMisReview(tMisReview));
    }

    /**
     * 删除检阅信息
     */
    //    @PreAuthorize("@ss.hasPermi('spspublic:review:remove')")
    @Log(title = "检阅信息", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{ctIds}")
    public AjaxResult remove(@PathVariable String[] ctIds) {
        return toAjax(tMisReviewService.deleteTMisReviewByIds(ctIds));
    }
}
